using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Linq;
using System.Reflection;
[CustomPropertyDrawer(typeof(BehTree.MyCondition), true)]
public class ConditionDrawer : PropertyDrawer
{
    GameObject lastObj;
    int methodIndex;
    int lastIndex;
    string[] methods;
    Dictionary<string, Component> methodDic = new Dictionary<string, Component>();
    void ReturnScript(SerializedProperty property, string name,Component tar)
    {
		property.serializedObject.ApplyModifiedProperties();
        SerializedProperty methodName = property.FindPropertyRelative("methodName");
        SerializedProperty target = property.FindPropertyRelative("target");
        methodName.stringValue=name;
        target.objectReferenceValue=tar;
    }
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        Rect totalRect = new Rect(position.x,position.y,position.width,EditorGUIUtility.singleLineHeight);
        Rect targetRect=new Rect(position.x,position.y,position.width/2*0.8f,EditorGUIUtility.singleLineHeight);
        Rect methodRect=new Rect(targetRect.x+position.width/2,position.y,position.width/2*0.8f,position.height);
		EditorGUI.BeginProperty(totalRect, label, property);
        // EditorGUILayout.BeginHorizontal();
        SerializedProperty obj = property.FindPropertyRelative("obj");
        SerializedProperty target = property.FindPropertyRelative("target");
        
        // EditorGUI.ObjectField(position,obj);
        EditorGUI.PropertyField(targetRect,obj,GUIContent.none);
        GameObject curObj=obj.objectReferenceValue as GameObject;
        
        if (curObj != null)
        {
            if (lastObj != curObj||target.objectReferenceValue==null)
            {
                GetMethodNames(curObj);
                ReturnScript(property, methods[methodIndex], methodDic[methods[methodIndex]]);
            }
            methodIndex = EditorGUI.Popup(methodRect, methodIndex, methods);
            if (lastIndex != methodIndex)
            {
                ReturnScript(property,methods[methodIndex],methodDic[methods[methodIndex]]);
                lastIndex = methodIndex;
            }
        }
        else
        {
            EditorGUI.LabelField(methodRect,"未选择目标对象");
        }
        // EditorGUILayout.EndHorizontal();
        EditorGUI.EndProperty();
    }
    
    public void GetMethodNames(GameObject obj)
    {
        methodDic.Clear();
        List<string> nameList = new List<string>();
        foreach (var c in obj.GetComponents<Component>())
            if (c.GetType() != typeof(GameObject))
                foreach (var e in c.GetType().GetMethods().Distinct<MethodInfo>())
                    if (e.ReturnType == typeof(bool) &&
                        e.DeclaringType.Name == c.GetType().Name &&
                        e.GetParameters().Length == 0)
                    {
                        nameList.Add(e.Name);
                        if (!methodDic.ContainsKey(e.Name))
                            methodDic.Add(e.Name, c);
                    }
        methods = new string[nameList.Count];
        for (int i = 0; i < nameList.Count; i++)
            methods[i] = nameList[i];

    }
}
